package com.block.risk.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.common.exception.NotifyExceptionService;
import com.block.common.service.LogFactory;
import com.block.db.entity.RiskConfigInfo;
import com.block.db.entity.RiskCostRecordInfo;
import com.block.db.mapper.RiskCostRecordInfoMapper;
import com.block.risk.enums.ReqActionEnum;
import com.block.risk.service.IRiskCostRecordInfoService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 风控消费记录 服务实现类
 * </p>
 *
 * @author
 * @since 2023-05-27
 */
@Service
public class RiskCostRecordInfoServiceImpl extends ServiceImpl<RiskCostRecordInfoMapper, RiskCostRecordInfo> implements IRiskCostRecordInfoService {

    private static Logger log = LogFactory.getRiskLog();
    @Autowired
    NotifyExceptionService notifyExceptionService;

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public boolean saveOrUpdateCostRecordInfo(String dateInfo, ReqActionEnum reqActionEnum, RiskConfigInfo riskConfigInfo) {

        try {
            RiskCostRecordInfo costRecordInfo = this.baseMapper.getEntityByDateInfoAndConfigIdLock(dateInfo, riskConfigInfo.getId());
            if (ObjectUtil.isEmpty(costRecordInfo)) {
                try {
                    costRecordInfo = this.saveNewEntity(dateInfo, riskConfigInfo);
                    if (ObjectUtil.isEmpty(costRecordInfo)) {
                        log.info("【风控请求统计】新增统计信息为空【dateInfo】{}【configId】{}", dateInfo, riskConfigInfo.getId());
                        return false;
                    }
                } catch (Exception exception) {
                    log.error("【风控请求统计】新增统计信息异常", exception);
                    String content = StrUtil.format("【dateInfo】{}【configId】{}", dateInfo, riskConfigInfo.getId());
                    notifyExceptionService.notifyNoticeInfo("RISK", "新增统计信息异常", content, exception);
                }
            }

            RiskCostRecordInfo updateEntity = new RiskCostRecordInfo();
            updateEntity.setId(costRecordInfo.getId());
            updateEntity.setUpdateTime(LocalDateTime.now());
            /**
             * 根据操作类型更新数据信息
             */
            if (StrUtil.equals(reqActionEnum.getValue(), ReqActionEnum.INIT.getValue())) {
                updateEntity.setRequestNumber(1);
            } else if (StrUtil.equals(reqActionEnum.getValue(), ReqActionEnum.FINISH.getValue())) {
                updateEntity.setRequestNumber(1);
                updateEntity.setFinishNumber(1);
            } else if (StrUtil.equals(reqActionEnum.getValue(), ReqActionEnum.COST.getValue())) {
                updateEntity.setRequestNumber(1);
                updateEntity.setFinishNumber(1);
                updateEntity.setChargeNumber(1);
                updateEntity.setCostAmount(riskConfigInfo.getPriceAmount());
            } else if (StrUtil.equals(reqActionEnum.getValue(), ReqActionEnum.FREE.getValue())) {
                updateEntity.setRequestNumber(1);
                updateEntity.setFinishNumber(1);
                updateEntity.setFreeNumber(1);
            }

            int updateRes = this.baseMapper.updateEntityById(updateEntity);
            if (updateRes > 0) {
                return true;
            }
            return false;
        } catch (Exception exception) {
            log.error("【风控请求统计】风控统计异常", exception);
            String content = StrUtil.format("【dateInfo】{}【configId】{}", dateInfo, riskConfigInfo.getId());
            notifyExceptionService.notifyNoticeInfo("RISK", "风控统计异常", content, exception);
        }
        return false;
    }

    private RiskCostRecordInfo saveNewEntity(String dateInfo, RiskConfigInfo riskConfigInfo) {
        RiskCostRecordInfo newEntity = new RiskCostRecordInfo();
        newEntity.setDateInfo(dateInfo);
        newEntity.setConfigId(riskConfigInfo.getId());
        newEntity.setConfigName(riskConfigInfo.getName());

        boolean save = this.save(newEntity);
        if (!save) {
            return null;
        }
        return this.baseMapper.getEntityByDateInfoAndConfigIdLock(dateInfo, riskConfigInfo.getId());
    }

}
